package com.fz.us.oa.web.controller.main.company;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.jqgrid.JqGridFilter;
import com.fz.us.admin.base.bean.jqgrid.JqGridPager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PageHelp;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.base.utils.EntityConfig;
import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.CommonConfigService;
import com.fz.us.admin.company.service.ProcessBriefService;
import com.fz.us.admin.company.service.RemindService;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.fz.us.oa.web.spring.bind.annotation.RequestJsonParam;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * 控制器 - 岗位信息
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-09 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/remind")
public class RemindController extends BaseController {


    private static final long serialVersionUID = 3851349105893900794L;

    @Autowired
    private RemindService remindService;


    /**
     * 列表
     */
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> list(@CurrentUser User user,
                                    JqGridPager pager,
                                    @RequestJsonParam(value = "filters", required = false) JqGridFilter filters) {

        Map<String,Object> params = new HashMap<String, Object>();
        params.put("state", BaseEnumManager.StateEnum.Enable);
        params.put("user", user);
        List<Remind> remindList = remindService.getList(params);
        ArrayList<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;
        for(Remind remind: remindList){
            String bussinessKey =  remind.getBussinessKey();
            String bussinessId =  remind.getKeyId();
            String url = "";
            if(StringUtils.isNotEmpty(bussinessKey)){
                String rs = (new StringBuilder()).append(Character.toLowerCase(bussinessKey.charAt(0))).append(bussinessKey.substring(1)).toString();
                String key =   rs.substring( rs.lastIndexOf(".")+1);
                url =  "../com/" + key + "/read?keyId=" +bussinessId + "&remind=1" ;
            }
            rMap = new HashMap<String, Object>();
            rMap.put("content",remind.getContent());
            rMap.put("id", remind.getId());
            rMap.put("time", DataUtil.DateToString(remind.getCreateTime(), "yyyy-MM-dd"));
            rMap.put("url",url);
            dataRows.add(rMap);
        }
        //不满10条，补充
        for(int i=0;i<10-remindList.size();i++){
            rMap = new HashMap<String, Object>();
            rMap.put("state", "");
            rMap.put("user", "");
            rMap.put("content", "");
            rMap.put("id", "");
            rMap.put("time", "");
            rMap.put("url", "");
            dataRows.add(rMap);
        }
        data.put("dataRows", dataRows);
        return data;
    }


}
